package com.lxmes.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lxmes.business.domain.CcCaigouMain;
import com.lxmes.business.domain.CcRukuMain;
import com.lxmes.business.service.ICcChukuDetailService;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.system.service.ISysDictDataService;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.business.domain.CcChukuMain;
import com.lxmes.business.service.ICcChukuMainService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 出库单据主表Controller
 * 
 * @author 王超
 * @date 2022-06-29
 */
@Api(tags="出库单")
@RestController
@RequestMapping("/business/chukumain")
public class CcChukuMainController extends BaseController
{
    @Autowired
    private ICcChukuMainService ccChukuMainService;
    @Autowired
    private ICcChukuDetailService ccChukuDetailService;
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询出库单据主表列表
     */
    @PreAuthorize("@ss.hasPermi('business:chukumain:list')")
    @GetMapping("/list")
    public TableDataInfo list(CcChukuMain ccChukuMain)
    {
        startPage();
        ccChukuMain.setDeptId(SecurityUtils.getDeptId());
        List<CcChukuMain> list = ccChukuMainService.selectCcChukuMainList(ccChukuMain);
        return getDataTable(list);
    }
    /**
     * 手机端出库单列表
     */
    @ApiOperation(value="出库单列表",tags = "chukuFlag出库方式 1-原材料 2-产成品")
    //查询条件
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryParams", value = "出库单号、经办人", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "chukuFlag", value = "出库方式", paramType="query", dataType = "String",required = true,dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",  required = true,paramType="query",  dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum", value = "当前页数",  required = true,paramType="query",   dataType = "int", dataTypeClass = Integer.class)
    })
    @ApiResponses({@ApiResponse(code=200,message = "0k",response = CcChukuMain.class)})
    @PreAuthorize("@ss.hasPermi('mobile:chukumain:list')")
    @GetMapping("/mobileList")
    public TableDataInfo mobileList(@RequestParam(value ="queryParams",required = false) String queryParams,@RequestParam(value ="chukuFlag",required = true) String chukuFlag)
    {
        startPage();
        CcChukuMain ccChukuMain=new CcChukuMain();
        ccChukuMain.setChukuFlag(chukuFlag);
        ccChukuMain.setQueryParams(queryParams);
        List<CcChukuMain> list = ccChukuMainService.selectCcChukuMainList(ccChukuMain);
        return getDataTable(list);
    }
    /**
     * 手机端新增出库
     */
    @ApiOperation(value="新增出库任务",tags = "chukuFlag出库方式 1-原材料 2-产成品")
    @Log(title = "新增出库任务", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('mobile:chukumain:mobileAdd')")
    @PostMapping("/mobileAdd")
    public AjaxResult mobileAdd(@RequestBody CcChukuMain ccChukuMain)
    {
        return toAjax(ccChukuMainService.insertCcChukuMain(ccChukuMain));
    }
    @ApiOperation(value="获取出库类型")
    @GetMapping("/getDictDataCuKuType")
    public AjaxResult getDictDataCuKuType(){
        return AjaxResult.success( dictDataService.selectDictDataByType("chuku_type"));
    }
    /**
     * 导出出库单据主表列表
     */
    @PreAuthorize("@ss.hasPermi('business:chukumain:export')")
    @Log(title = "出库单据主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CcChukuMain ccChukuMain)
    {
        ccChukuMain.setDeptId(SecurityUtils.getDeptId());
        List<CcChukuMain> list = ccChukuMainService.selectCcChukuMainList(ccChukuMain);
        ExcelUtil<CcChukuMain> util = new ExcelUtil<CcChukuMain>(CcChukuMain.class);
        util.exportExcel(response, list, "出库单据主表数据");
    }

    /**
     * 获取出库单据主表详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:chukumain:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ccChukuMainService.selectCcChukuMainById(id));
    }
    @GetMapping("/getChukumainBychukuFlag/{chukuFlag}")
    public AjaxResult list(@PathVariable("chukuFlag") String chukuFlag)
    {
        List<CcChukuMain> list = ccChukuMainService.selectChukumainBychukuFlag(chukuFlag);
        JSONArray jsonArray=new JSONArray();
        for (CcChukuMain main:list) {
            JSONObject object=new JSONObject();
            object.put("value",main.getDanhao());
            object.put("label",main.getOrderTitle());
            object.put("cusName",main.getCusName());
            object.put("cusId",main.getCusId());
            object.put("orderId",main.getOrderId());
            object.put("money",main.getTotalPrice());
            jsonArray.add(object);
        }
        return AjaxResult.success(jsonArray);
    }
    /**
     * 获取出库任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:chukumain:zhixing')")
    @GetMapping(value = "/zhixingChuku/{danhao}")
    public AjaxResult zhixingChuku(@PathVariable("danhao") String danhao)
    {
        return AjaxResult.success(ccChukuMainService.zhixingChuku(danhao));
    }
    /**
     * 手机端获取出库详细信息
     */
    @ApiOperation(value="出库单详细信息")
    @ApiImplicitParam(name = "danhao", value = "出库单号", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @GetMapping(value = "/getMobileChuKuMain/{danhao}")
    public AjaxResult mobileGetInfo(@PathVariable("danhao") String  danhao)
    {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("chukuMain",ccChukuMainService.selectCcChukuMainByDanHao(danhao));
        jsonObject.put("chukuDetail", ccChukuDetailService.selectCcChukuDetailByDanHao(danhao));
        return AjaxResult.success(jsonObject);
    }

    /**
     * 新增出库单据主表
     */
    @PreAuthorize("@ss.hasPermi('business:chukumain:add')")
    @Log(title = "出库单据主表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CcChukuMain ccChukuMain)
    {
        return toAjax(ccChukuMainService.insertCcChukuMain(ccChukuMain));
    }

    /**
     * 修改出库单据主表
     */
    @PreAuthorize("@ss.hasPermi('business:chukumain:edit')")
    @Log(title = "出库单据主表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CcChukuMain ccChukuMain)
    {
        return toAjax(ccChukuMainService.updateCcChukuMain(ccChukuMain));
    }

    /**
     * 删除出库单据主表
     */
    @PreAuthorize("@ss.hasPermi('business:chukumain:remove')")
    @Log(title = "出库单据主表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{danhao}")
    public AjaxResult remove(@PathVariable String danhao)
    {
        return toAjax(ccChukuMainService.deleteCcChukuMainByDanHao(danhao));
    }
}
